<?php
namespace App\Common\Cla\Web;

use App\Common as com;

class PasswordMix extends com\Cla\ClaBase {

	/**
	 * 明文密码
	 * @var string
	 */
	private string $password;

	/**
	 * 加密盐
	 * @var string
	 */
	private string $saltKey;

	/**
	 * Hash constructor.
	 * @param string $password 明文密码
	 * @param string $saltKey 加密盐
	 * @throws
	 */
	public function __construct( string $password, string $saltKey = '' ) {
		// ...
		if( empty( $password ) ){
			throw com\Lib\Err\Exception::makeOfNew( '参数->密码不能为空' );
		}
		//
		$this->password = $password;
		$this->saltKey = $saltKey;
		//
		parent::__construct();
	}

	/**
	 * 验证PasswordHash
	 * @param string $passwordMix 待验证hash
	 * @return bool
	 */
	public function verify( string $passwordMix ):bool {
		return $this->buildPasswordMd5() === $passwordMix;
	}

	/**
	 * 生成密码md5
	 * @return string
	 * @throws
	 */
	private function buildPasswordMd5():string {
		return md5( md5( $this->password ) . md5( $this->saltKey ) . com\Cla\Web::getWebSaltKey() );
	}
	
	// ===============================
	//
	//
	// 下面为静态方法
	//
	//
	// ===============================

	/**
	 * 快速获取密码md5
	 * @param string $password 明文密码
	 * @param string $saltKey 加密盐
	 * @return string
	 * @throws
	 */
	public static function getPasswordMd5( string $password, string $saltKey = '' ):string {
		return (new static( $password, $saltKey ))->buildPasswordMd5();
	}

}
